window.addEventListener('load', function() {
    const sideBarList = this.document.querySelector('.side-bar-list');
    let added = false; 

    window.addEventListener('scroll', function() {
        const scrollTop = document.documentElement.scrollTop;

        if (scrollTop >= 800 && !added) {
            let sideBarChild = document.createElement('li');
            sideBarChild.classList.add('side-bar-child');
         
            let img = document.createElement('img');
            img.src = './img/side6.png'
            sideBarChild.appendChild(img);
            
            let text = document.createElement('p');
            text.textContent = '回顶部';
            sideBarChild.appendChild(text);

            sideBarList.appendChild(sideBarChild);
           
            sideBarChild.addEventListener('click', function() {
                window.scrollTo({
                    top: 0,
                    behavior: 'smooth' 
                });
            });

            added = true; 
        } else if (scrollTop < 800 && added) {
            sideBarList.removeChild(sideBarList.lastChild); 
            added = false; 
        }
    });
});